package com.bootx.controller.api;

import com.bootx.common.Result;
import com.bootx.controller.BaseController;
import com.bootx.entity.BaseEntity;
import com.bootx.service.ProductService;
import com.fasterxml.jackson.annotation.JsonView;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author black
 */
@RestController
@RequestMapping("/api/product")
public class ProductController extends BaseController {

    @Resource
    private ProductService productService;

    @PostMapping("/all")
    @JsonView({BaseEntity.TreeView.class})
    public Result tree(){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select productCategories_id `id`,productcategory.name from product_productcategories,productcategory where productcategory.id=product_productcategories.productCategories_id group by productCategories_id");
        maps.forEach(map -> {
            map.put("list",jdbcTemplate.queryForList("select id,name,image,memo from product where id in (select products_id from product_productcategories where productCategories_id=?)",map.get("id")));
        });
        return Result.success(maps);
    }

    @PostMapping("/detail")
    @JsonView({BaseEntity.ViewView.class})
    public Result detail(Long id){
        return Result.success(productService.find(id));
    }

}
